package com.ak.demo.config;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;


/**
 * 访问在添加的全部链接
 * 每65 秒刷新一次
 */
@Slf4j
public class VisitJob implements Job {


    public  String USER_AGENT = "User-Agent";
    public String USER_AGENT_VALUE = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0";


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        log.info("链接的数量");
        if(UrlComponent.list.size()>0){
            for(String url :UrlComponent.list){
                try {
                    Jsoup.connect(url)
                            .header(USER_AGENT,USER_AGENT_VALUE)
                            .get();
                    log.info("访问了"+url);
                } catch (IOException e) {
                    log.info("链接发生错误");
                    // 移除连接
                    UrlComponent.list.remove(url);
                }
            }
        }else{
            log.info("对不起visit job 错误");
        }
        // 次数增加
        UrlComponent.count++;
    }
}
